package CodingBatExamples.warmup2;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: myubuntu
 * Date: 11/18/13
 * Time: 1:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class WarmUp2 {

    public static boolean doubleX(String str){
        //Given a string, return true if the first instance of "x" in the string is immediately followed by another "x"
        if((str.indexOf('x') != -1) && (str.length() > 1)){
            int indexOfFirstX = str.indexOf('x');
            if(indexOfFirstX == str.length()-1){
                return false;
            }

            if(str.charAt(indexOfFirstX +1) == 'x'){
                return  true;
            }
        }


        return false;
    }

    public static String stringBits(String str){
        //Given a string, return a new string made of every other char starting with the first, so "Hello' yields "Hlo"
        String newString = "";
        for(int i=0;i<=str.length()-1;i=i+2){
            newString = newString + str.substring(i,i+1);
        }
        return newString;
    }

    public static String stringSplosion(String str){
        String newString = "";
        for(int i=0; i<str.length(); i++){
            newString = newString + str.substring(0,i+1);
        }
        return newString;
    }

    public static int last2(String str){
/*    Given a string, return the count of the number of times that a substring length 2 appears in the string
    and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring). */
        int numOfOccurrences = 0;
        if(str.length() <= 2){
            numOfOccurrences = 0;
        }
        else{
            String last2Chars = str.substring(str.length()-2);
            for(int i=0; i<str.length()-2; i++){
                if(str.substring(i,i+2).equals(last2Chars)){
                    numOfOccurrences++;
                }
            }
        }
        return numOfOccurrences;
    }

    public static int arrayCount9(int[] nums){
        //Given an array of ints, return the number of 9's in the array.
        if(nums.length < 1){
            return 0;
        }
        int numOfNines = 0;
        for(int numbers:nums){
            if(numbers == 9){
                numOfNines++;
            }
        }
        return numOfNines;
    }

    public static boolean arrayFront9(int[] nums){
/*Given an array of ints, return true if one of the
    first 4 elements in the array is a 9. The array length may be less than 4.*/
        int end = nums.length;
        if(nums.length > 4){
            end = 4;
        }
        for(int i=0; i< end;i++){
            if(nums[i] == 9){
                return true;
            }
        }
        return false;
    }

    public static boolean array123(int[] nums){
        //Given an array of ints, return true if 1, 2, 3, appears in the array somewhere.
        int[] oneTwoThree = {1,2,3};
        int[] subArray = new int[3];
        for(int i=0;i<(nums.length-2);i++){
            subArray = Arrays.copyOfRange(nums,i,i+3);
            if(Arrays.equals(subArray,oneTwoThree)){
                return true;
            }
        }
        return false;
    }

    public static int stringMatch(String a, String b){
    /*Given 2 strings, a and b, return the number of the positions where they contain
    the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx",
    "aa", and "az" substrings appear in the same place in both strings.*/
        int hits = 0;

        int end;

        String shorterWord = "";
        if(a.length() > b.length()){
          shorterWord = b;
        }
        else{
          shorterWord = a;
        }

        for(int i=0; i< shorterWord.length()-1;i++){
            if(a.substring(i,i+2).equals(b.substring(i,i+2))){
            hits++;
          }
        }
        return hits;
    }

    public static String stringX(String str){
        String myString = str;
        return myString;
    }

    public static void main(String[] args) {
        System.out.println("danie");
    }
}

